package com.luxx.util;

import org.apache.commons.lang3.time.FastDateFormat;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


/**
 * @author luxx
 */
public class DateUtil {
    /**
     * 默认日期时间格式
     */
    public final static FastDateFormat datetimeFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
    public final static FastDateFormat timestampFormat = FastDateFormat.getInstance("yyyyMMddHHmmss");
    public final static FastDateFormat dateFormat = FastDateFormat.getInstance("yyyy-MM-dd");
    public final static FastDateFormat timeFormat = FastDateFormat.getInstance("HH:mm:ss");

    /**
     * @return 当前时间 yyyy-MM-dd HH:mm:ss
     */
    public static String datetime() {
        return datetimeFormat.format(new Date());
    }

    /**
     * @return 当前时间 yyyy-MM-dd
     */
    public static String date() {
        return dateFormat.format(new Date());
    }

    /**
     * @return 当前时间 HH:mm:ss
     */
    public static String time() {
        return timeFormat.format(new Date());
    }

    /**
     * @return 当前时间 yyyyMMddHHmmss
     */
    public static String timestampString() {
        return timestampFormat.format(new Date());
    }

    /**
     * @return 当前时间 Calendar
     */
    public static Calendar getCalendar() {
        return Calendar.getInstance(Locale.CHINA);
    }

    /**
     * @return 当前时间 Calendar
     */
    public static Date getToday() {
        Calendar calendar = getCalendar();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * @param field  the given calendar field.
     * @param amount the amount of date or time to be added to the field.
     * @return
     * @see Calendar#DAY_OF_MONTH  etc
     */
    public static Calendar getCalendar(int field, int amount) {
        Calendar calendar = getCalendar();
        calendar.add(field, amount);
        return calendar;
    }

    public static void main(String[] args) throws ParseException {
        System.out.println(getCalendar(1, 1));
        ///** 设定本周五   12:00:00.000     */
        //Calendar calendar = getCalendar();
        //calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
        //calendar.set(Calendar.HOUR_OF_DAY, 12);
        //calendar.set(Calendar.MINUTE, 0);
        //calendar.set(Calendar.SECOND, 0);
        //calendar.set(Calendar.MILLISECOND, 0);
        //
        //Date d1, d2, now = new Date();
        //if (calendar.getTime().after(now)) {
        //    d2 = calendar.getTime();
        //    calendar.add(Calendar.DAY_OF_YEAR, -7);
        //    d1 = calendar.getTime();
        //} else {
        //    d1 = calendar.getTime();
        //    calendar.add(Calendar.DAY_OF_YEAR, 7);
        //    d2 = calendar.getTime();
        //}
        //Calendar c1 = Calendar.getInstance();
        //c1.setTime(d1);
        //Calendar c2 = Calendar.getInstance();
        //c2.setTime(d2);
        //System.out.println((c1.get(Calendar.MONTH) + 1) + "-" + c1.get(Calendar.DAY_OF_MONTH));
        //System.out.println((c2.get(Calendar.MONTH) + 1) + "-" + c2.get(Calendar.DAY_OF_MONTH));
    }

    public static Date addDay(Date now, int num) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.DAY_OF_MONTH, num);
        return cal.getTime();
    }

    public static Date tomorrow() {
        return addDay(new Date(), 1);
    }

    public static Date tomorrow(Date now) {
        return addDay(now, 1);
    }
}